home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume89 / unix / gnused.2 < prev    next >
Internet Message Format  |  1989-07-12  |  74KB

  1. Path: xanth!ames!apple!sun-barr!newstop!sun!swap!page
  2. From: page%swap@Sun.COM (Bob Page)
  3. Newsgroups: comp.sources.amiga
  4. Subject: v89i166:  gnused -gnu stream/script editor, Part02/03
  5. Message-ID: <115135@sun.Eng.Sun.COM>
  6. Date: 12 Jul 89 23:14:25 GMT
  7. Sender: news@sun.Eng.Sun.COM
  8. Lines: 2188
  9. Approved: page@sun.com
  10.  
  11. Submitted-by: ehoogerbeets@rose.waterloo.edu (Edwin Hoogerbeets)
  12. Posting-number: Volume 89, Issue 166
  13. Archive-name: unix/gnused.2
  14.  
  15. # This is a shell archive.
  16. # Remove anything above and including the cut line.
  17. # Then run the rest of the file through 'sh'.
  18. # Unpacked files will be owned by you and have default permissions.
  19. #----cut here-----cut here-----cut here-----cut here----#
  20. #!/bin/sh
  21. # shar: SHell ARchive
  22. # Run the following text through 'sh' to create:
  23. #    makefile
  24. #    regex.c
  25. #    regex.h
  26. # This is archive 2 of a 3-part kit.
  27. # This archive created: Wed Jul 12 15:53:24 1989
  28. echo "extracting makefile"
  29. sed 's/^X//' << \SHAR_EOF > makefile
  30. X#
  31. X# Makefile for Gnu Sed (see public license at the top of sed.c)
  32. X#
  33. X
  34. X# add getopt.o and/or alloca.o if these are not present on your system
  35. XOBJS = sed.o regex.o glob.o dir.o getopt.o alloca.o
  36. X
  37. XSRC = sed.c regex.c glob.c regex.h Makefile
  38. X
  39. X# on SysV systems, comment out the next line and comment out the one below it
  40. X#CFLAGS = -g -DUSG -I.
  41. X#CFLAGS = -g -I.
  42. X#CFLAGS = -O -I.
  43. X
  44. XCFLAGS=+L -DAMIGA
  45. X# -n for Manx debugging
  46. X
  47. XLNFLAGS=
  48. X# -g for Manx debugging
  49. X
  50. X# on unix machines:
  51. X# LN=cc
  52. X# on the amiga with Manx C:
  53. XLN=ln
  54. X
  55. XLIBS=dh0:usr/lib/heapmem.o32 -lc32
  56. XBINDIR=dh0:usr/local/bin
  57. X
  58. Xsed:    $(OBJS)
  59. X        $(LN) $(LNFLAGS) -o $@ $(OBJS) $(LIBS)
  60. X
  61. Xclean:
  62. X        rm -f sed $(OBJS) core
  63. X
  64. Xdist:   sed.tar.Z
  65. X
  66. Xsed.tar.Z:
  67. X        tar cvzf sed.tar.Z $(SRC)
  68. X
  69. Xinstall: sed
  70. X        cp -f sed $(BINDIR)
  71. X
  72. X
  73. SHAR_EOF
  74. echo "extracting regex.c"
  75. sed 's/^X//' << \SHAR_EOF > regex.c
  76. X/* Extended regular expression matching and search.
  77. X   Copyright (C) 1985 Free Software Foundation, Inc.
  78. X
  79. X                       NO WARRANTY
  80. X
  81. X  BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  82. XNO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  83. XWHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  84. XRICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  85. XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  86. XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  87. XFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  88. XAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  89. XDEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  90. XCORRECTION.
  91. X
  92. X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  93. XSTALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  94. XWHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  95. XLIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  96. XOTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  97. XUSE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  98. XDATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  99. XA FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  100. XPROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  101. XDAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  102. X
  103. X                GENERAL PUBLIC LICENSE TO COPY
  104. X
  105. X  1. You may copy and distribute verbatim copies of this source file
  106. Xas you receive it, in any medium, provided that you conspicuously and
  107. Xappropriately publish on each copy a valid copyright notice "Copyright
  108. X(C) 1985 Free Software Foundation, Inc."; and include following the
  109. Xcopyright notice a verbatim copy of the above disclaimer of warranty
  110. Xand of this License.  You may charge a distribution fee for the
  111. Xphysical act of transferring a copy.
  112. X
  113. X  2. You may modify your copy or copies of this source file or
  114. Xany portion of it, and copy and distribute such modifications under
  115. Xthe terms of Paragraph 1 above, provided that you also do the following:
  116. X
  117. X    a) cause the modified files to carry prominent notices stating
  118. X    that you changed the files and the date of any change; and
  119. X
  120. X    b) cause the whole of any work that you distribute or publish,
  121. X    that in whole or in part contains or is a derivative of this
  122. X    program or any part thereof, to be licensed at no charge to all
  123. X    third parties on terms identical to those contained in this
  124. X    License Agreement (except that you may choose to grant more extensive
  125. X    warranty protection to some or all third parties, at your option).
  126. X
  127. X    c) You may charge a distribution fee for the physical act of
  128. X    transferring a copy, and you may at your option offer warranty
  129. X    protection in exchange for a fee.
  130. X
  131. XMere aggregation of another unrelated program with this program (or its
  132. Xderivative) on a volume of a storage or distribution medium does not bring
  133. Xthe other program under the scope of these terms.
  134. X
  135. X  3. You may copy and distribute this program (or a portion or derivative
  136. Xof it, under Paragraph 2) in object code or executable form under the terms
  137. Xof Paragraphs 1 and 2 above provided that you also do one of the following:
  138. X
  139. X    a) accompany it with the complete corresponding machine-readable
  140. X    source code, which must be distributed under the terms of
  141. X    Paragraphs 1 and 2 above; or,
  142. X
  143. X    b) accompany it with a written offer, valid for at least three
  144. X    years, to give any third party free (except for a nominal
  145. X    shipping charge) a complete machine-readable copy of the
  146. X    corresponding source code, to be distributed under the terms of
  147. X    Paragraphs 1 and 2 above; or,
  148. X
  149. X    c) accompany it with the information you received as to where the
  150. X    corresponding source code may be obtained.  (This alternative is
  151. X    allowed only for noncommercial distribution and only if you
  152. X    received the program in object code or executable form alone.)
  153. X
  154. XFor an executable file, complete source code means all the source code for
  155. Xall modules it contains; but, as a special exception, it need not include
  156. Xsource code for modules which are standard libraries that accompany the
  157. Xoperating system on which the executable file runs.
  158. X
  159. X  4. You may not copy, sublicense, distribute or transfer this program
  160. Xexcept as expressly provided under this License Agreement.  Any attempt
  161. Xotherwise to copy, sublicense, distribute or transfer this program is void and
  162. Xyour rights to use the program under this License agreement shall be
  163. Xautomatically terminated.  However, parties who have received computer
  164. Xsoftware programs from you with this License Agreement will not have
  165. Xtheir licenses terminated so long as such parties remain in full compliance.
  166. X
  167. X  5. If you wish to incorporate parts of this program into other free
  168. Xprograms whose distribution conditions are different, write to the Free
  169. XSoftware Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  170. Xworked out a simple rule that can be stated here, but we will often permit
  171. Xthis.  We will be guided by the two goals of preserving the free status of
  172. Xall derivatives of our free software and of promoting the sharing and reuse of
  173. Xsoftware.
  174. X
  175. X
  176. XIn other words, you are welcome to use, share and improve this program.
  177. XYou are forbidden to forbid anyone else to use, share and improve
  178. Xwhat you give them.   Help stamp out software-hoarding!  */
  179. X
  180. X
  181. X/* To test, compile with -Dtest.
  182. X This Dtestable feature turns this into a self-contained program
  183. X which reads a pattern, describes how it compiles,
  184. X then reads a string and searches for it.  */
  185. X
  186. X#ifdef emacs
  187. X
  188. X/* The `emacs' switch turns on certain special matching commands
  189. X that make sense only in emacs. */
  190. X
  191. X#include "config.h"
  192. X#include "lisp.h"
  193. X#include "buffer.h"
  194. X#include "syntax.h"
  195. X
  196. X#else  /* not emacs */
  197. X
  198. X#ifdef USG
  199. X#define bcopy(s,d,n)    memcpy((d),(s),(n))
  200. X#define bcmp(s1,s2,n)   memcmp((s1),(s2),(n))
  201. X#define bzero(s,n)      memset((s),0,(n))
  202. X#endif /* USG */
  203. X
  204. X#ifdef AZTEC_C
  205. X#define bcopy(src,dst,n)  movmem((src),(dst),(n))
  206. X#define memcpy(dst,src,n) movmem((src),(dst),(n))
  207. X#define bcmp(s1,s2,n)   memcmp((s1),(s2),(n))
  208. X#define memset(src,value,howmany) setmem((src),(howmany),(value))
  209. X#define bzero(src,howmany) setmem((src),(howmany),0)
  210. X
  211. Xextern unsigned char *alloca();
  212. X
  213. X#endif /* AZTEC_C */
  214. X
  215. X/* Make alloca work the best possible way.  */
  216. X#ifdef __GNUC__
  217. X#define alloca __builtin_alloca
  218. X#else
  219. X#ifdef sparc
  220. X#include <alloca.h>
  221. X#endif
  222. X#endif
  223. X
  224. Xextern char *realloc();
  225. Xextern char *malloc();
  226. X
  227. X#ifdef AZTEC_C
  228. Xint memcmp(a,b,length)
  229. Xchar *a, *b;
  230. Xint length;
  231. X{
  232. X  register int index;
  233. X
  234. X  for ( index = 0; index < length; index++ ) {
  235. X    if ( a[index] != b[index] ) {
  236. X      return(a[index] > b[index] ? -1 : 1);
  237. X    }
  238. X  }
  239. X
  240. X  return(0);
  241. X}
  242. X#endif
  243. X
  244. X/*
  245. X * Define the syntax stuff, so we can do the \<...\> things.
  246. X */
  247. X
  248. X#ifndef Sword /* must be non-zero in some of the tests below... */
  249. X#define Sword 1
  250. X#endif
  251. X
  252. X#define SYNTAX(c) re_syntax_table[c]
  253. X
  254. X#ifdef SYNTAX_TABLE
  255. X
  256. Xchar *re_syntax_table;
  257. X
  258. X#else
  259. X
  260. Xstatic char re_syntax_table[256];
  261. X
  262. Xstatic void
  263. Xinit_syntax_once ()
  264. X{
  265. X   register int c;
  266. X   static int done = 0;
  267. X
  268. X   if (done)
  269. X     return;
  270. X
  271. X   bzero (re_syntax_table, sizeof re_syntax_table);
  272. X
  273. X   for (c = 'a'; c <= 'z'; c++)
  274. X     re_syntax_table[c] = Sword;
  275. X
  276. X   for (c = 'A'; c <= 'Z'; c++)
  277. X     re_syntax_table[c] = Sword;
  278. X
  279. X   for (c = '0'; c <= '9'; c++)
  280. X     re_syntax_table[c] = Sword;
  281. X
  282. X   done = 1;
  283. X}
  284. X
  285. X#endif /* SYNTAX_TABLE */
  286. X#endif /* not emacs */
  287. X
  288. X#include "regex.h"
  289. X
  290. X/* Number of failure points to allocate space for initially,
  291. X when matching.  If this number is exceeded, more space is allocated,
  292. X so it is not a hard limit.  */
  293. X
  294. X#ifndef NFAILURES
  295. X#define NFAILURES 80
  296. X#endif /* NFAILURES */
  297. X
  298. X/* width of a byte in bits */
  299. X
  300. X#define BYTEWIDTH 8
  301. X
  302. X#ifndef SIGN_EXTEND_CHAR
  303. X#define SIGN_EXTEND_CHAR(x) (x)
  304. X#endif
  305. X
  306. Xstatic int obscure_syntax = 0;
  307. X
  308. X/* Specify the precise syntax of regexp for compilation.
  309. X   This provides for compatibility for various utilities
  310. X   which historically have different, incompatible syntaxes.
  311. X
  312. X   The argument SYNTAX is a bit-mask containing the two bits
  313. X   RE_NO_BK_PARENS and RE_NO_BK_VBAR.  */
  314. X
  315. Xint
  316. Xre_set_syntax (syntax)
  317. X{
  318. X  int ret;
  319. X
  320. X  ret = obscure_syntax;
  321. X  obscure_syntax = syntax;
  322. X  return ret;
  323. X}
  324. X
  325. X/* re_compile_pattern takes a regular-expression string
  326. X   and converts it into a buffer full of byte commands for matching.
  327. X
  328. X  PATTERN   is the address of the pattern string
  329. X  SIZE      is the length of it.
  330. X  BUFP      is a  struct re_pattern_buffer *  which points to the info
  331. X            on where to store the byte commands.
  332. X            This structure contains a  char *  which points to the
  333. X            actual space, which should have been obtained with malloc.
  334. X            re_compile_pattern may use  realloc  to grow the buffer space.
  335. X
  336. X  The number of bytes of commands can be found out by looking in
  337. X  the  struct re_pattern_buffer  that bufp pointed to,
  338. X  after re_compile_pattern returns.
  339. X*/
  340. X
  341. X#define PATPUSH(ch) (*b++ = (char) (ch))
  342. X
  343. X#define PATFETCH(c) \
  344. X {if (p == pend) goto end_of_pattern; \
  345. X  c = * (unsigned char *) p++; \
  346. X  if (translate) c = translate[c]; }
  347. X
  348. X#define PATFETCH_RAW(c) \
  349. X {if (p == pend) goto end_of_pattern; \
  350. X  c = * (unsigned char *) p++; }
  351. X
  352. X#define PATUNFETCH p--
  353. X
  354. X#define EXTEND_BUFFER \
  355. X  { char *old_buffer = bufp->buffer; \
  356. X    if (bufp->allocated == (1<<16)) goto too_big; \
  357. X    bufp->allocated *= 2; \
  358. X    if (bufp->allocated > (1<<16)) bufp->allocated = (1<<16); \
  359. X    if (!(bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated))) \
  360. X      goto memory_exhausted; \
  361. X    c = bufp->buffer - old_buffer; \
  362. X    b += c; \
  363. X    if (fixup_jump) \
  364. X      fixup_jump += c; \
  365. X    if (laststart) \
  366. X      laststart += c; \
  367. X    begalt += c; \
  368. X    if (pending_exact) \
  369. X      pending_exact += c; \
  370. X  }
  371. X
  372. Xstatic int store_jump (), insert_jump ();
  373. X
  374. Xchar *
  375. Xre_compile_pattern (pattern, size, bufp)
  376. X     char *pattern;
  377. X     int size;
  378. X     struct re_pattern_buffer *bufp;
  379. X{
  380. X  register char *b = bufp->buffer;
  381. X  register char *p = pattern;
  382. X  char *pend = pattern + size;
  383. X  register unsigned c, c1;
  384. X  char *p1;
  385. X  unsigned char *translate = (unsigned char *) bufp->translate;
  386. X
  387. X  /* address of the count-byte of the most recently inserted "exactn" command.
  388. X    This makes it possible to tell whether a new exact-match character
  389. X    can be added to that command or requires a new "exactn" command. */
  390. X
  391. X  char *pending_exact = 0;
  392. X
  393. X  /* address of the place where a forward-jump should go
  394. X    to the end of the containing expression.
  395. X    Each alternative of an "or", except the last, ends with a forward-jump
  396. X    of this sort. */
  397. X
  398. X  char *fixup_jump = 0;
  399. X
  400. X  /* address of start of the most recently finished expression.
  401. X    This tells postfix * where to find the start of its operand. */
  402. X
  403. X  char *laststart = 0;
  404. X
  405. X  /* In processing a repeat, 1 means zero matches is allowed */
  406. X
  407. X  char zero_times_ok;
  408. X
  409. X  /* In processing a repeat, 1 means many matches is allowed */
  410. X
  411. X  char many_times_ok;
  412. X
  413. X  /* address of beginning of regexp, or inside of last \( */
  414. X
  415. X  char *begalt = b;
  416. X
  417. X  /* Stack of information saved by \( and restored by \).
  418. X     Four stack elements are pushed by each \(:
  419. X       First, the value of b.
  420. X       Second, the value of fixup_jump.
  421. X       Third, the value of regnum.
  422. X       Fourth, the value of begalt.  */
  423. X
  424. X  int stackb[40];
  425. X  int *stackp = stackb;
  426. X  int *stacke = stackb + 40;
  427. X  int *stackt;
  428. X
  429. X  /* Counts \('s as they are encountered.  Remembered for the matching \),
  430. X     where it becomes the "register number" to put in the stop_memory command */
  431. X
  432. X  int regnum = 1;
  433. X
  434. X  bufp->fastmap_accurate = 0;
  435. X
  436. X#ifndef emacs
  437. X#ifndef SYNTAX_TABLE
  438. X  /*
  439. X   * Initialize the syntax table.
  440. X   */
  441. X   init_syntax_once();
  442. X#endif
  443. X#endif
  444. X
  445. X  if (bufp->allocated == 0)
  446. X    {
  447. X      bufp->allocated = 28;
  448. X      if (bufp->buffer)
  449. X        /* EXTEND_BUFFER loses when bufp->allocated is 0 */
  450. X        bufp->buffer = (char *) realloc (bufp->buffer, 28);
  451. X      else
  452. X        /* Caller did not allocate a buffer.  Do it for him */
  453. X        bufp->buffer = (char *) malloc (28);
  454. X      if (!bufp->buffer) goto memory_exhausted;
  455. X      begalt = b = bufp->buffer;
  456. X    }
  457. X
  458. X  while (p != pend)
  459. X    {
  460. X      if (b - bufp->buffer > bufp->allocated - 10)
  461. X        /* Note that EXTEND_BUFFER clobbers c */
  462. X        EXTEND_BUFFER;
  463. X
  464. X      PATFETCH (c);
  465. X
  466. X      switch (c)
  467. X        {
  468. X        case '$':
  469. X          if (obscure_syntax & RE_TIGHT_VBAR)
  470. X            {
  471. X              if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS) && p != pend)
  472. X                goto normal_char;
  473. X              /* Make operand of last vbar end before this `$'.  */
  474. X              if (fixup_jump)
  475. X                store_jump (fixup_jump, jump, b);
  476. X              fixup_jump = 0;
  477. X              PATPUSH (endline);
  478. X              break;
  479. X            }
  480. X
  481. X          /* $ means succeed if at end of line, but only in special contexts.
  482. X            If randomly in the middle of a pattern, it is a normal character. */
  483. X          if (p == pend || *p == '\n'
  484. X              || (obscure_syntax & RE_CONTEXT_INDEP_OPS)
  485. X              || (obscure_syntax & RE_NO_BK_PARENS
  486. X                  ? *p == ')'
  487. X                  : *p == '\\' && p[1] == ')')
  488. X              || (obscure_syntax & RE_NO_BK_VBAR
  489. X                  ? *p == '|'
  490. X                  : *p == '\\' && p[1] == '|'))
  491. X            {
  492. X              PATPUSH (endline);
  493. X              break;
  494. X            }
  495. X          goto normal_char;
  496. X
  497. X        case '^':
  498. X          /* ^ means succeed if at beg of line, but only if no preceding pattern. */
  499. X
  500. X          if (laststart && p[-2] != '\n'
  501. X              && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  502. X            goto normal_char;
  503. X          if (obscure_syntax & RE_TIGHT_VBAR)
  504. X            {
  505. X              if (p != pattern + 1
  506. X                  && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  507. X                goto normal_char;
  508. X              PATPUSH (begline);
  509. X              begalt = b;
  510. X            }
  511. X          else
  512. X            PATPUSH (begline);
  513. X          break;
  514. X
  515. X        case '+':
  516. X        case '?':
  517. X          if (obscure_syntax & RE_BK_PLUS_QM)
  518. X            goto normal_char;
  519. X        handle_plus:
  520. X        case '*':
  521. X          /* If there is no previous pattern, char not special. */
  522. X          if (!laststart && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  523. X            goto normal_char;
  524. X          /* If there is a sequence of repetition chars,
  525. X             collapse it down to equivalent to just one.  */
  526. X          zero_times_ok = 0;
  527. X          many_times_ok = 0;
  528. X          while (1)
  529. X            {
  530. X              zero_times_ok |= c != '+';
  531. X              many_times_ok |= c != '?';
  532. X              if (p == pend)
  533. X                break;
  534. X              PATFETCH (c);
  535. X              if (c == '*')
  536. X                ;
  537. X              else if (!(obscure_syntax & RE_BK_PLUS_QM)
  538. X                       && (c == '+' || c == '?'))
  539. X                ;
  540. X              else if ((obscure_syntax & RE_BK_PLUS_QM)
  541. X                       && c == '\\')
  542. X                {
  543. X                  int c1;
  544. X                  PATFETCH (c1);
  545. X                  if (!(c1 == '+' || c1 == '?'))
  546. X                    {
  547. X                      PATUNFETCH;
  548. X                      PATUNFETCH;
  549. X                      break;
  550. X                    }
  551. X                  c = c1;
  552. X                }
  553. X              else
  554. X                {
  555. X                  PATUNFETCH;
  556. X                  break;
  557. X                }
  558. X            }
  559. X
  560. X          /* Star, etc. applied to an empty pattern is equivalent
  561. X             to an empty pattern.  */
  562. X          if (!laststart)
  563. X            break;
  564. X
  565. X          /* Now we know whether 0 matches is allowed,
  566. X             and whether 2 or more matches is allowed.  */
  567. X          if (many_times_ok)
  568. X            {
  569. X              /* If more than one repetition is allowed,
  570. X                 put in a backward jump at the end.  */
  571. X              store_jump (b, maybe_finalize_jump, laststart - 3);
  572. X              b += 3;
  573. X            }
  574. X          insert_jump (on_failure_jump, laststart, b + 3, b);
  575. X          pending_exact = 0;
  576. X          b += 3;
  577. X          if (!zero_times_ok)
  578. X            {
  579. X              /* At least one repetition required: insert before the loop
  580. X                 a skip over the initial on-failure-jump instruction */
  581. X              insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  582. X              b += 3;
  583. X            }
  584. X          break;
  585. X
  586. X        case '.':
  587. X          laststart = b;
  588. X          PATPUSH (anychar);
  589. X          break;
  590. X
  591. X        case '[':
  592. X          while (b - bufp->buffer
  593. X                 > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  594. X            /* Note that EXTEND_BUFFER clobbers c */
  595. X            EXTEND_BUFFER;
  596. X
  597. X          laststart = b;
  598. X          if (*p == '^')
  599. X            PATPUSH (charset_not), p++;
  600. X          else
  601. X            PATPUSH (charset);
  602. X          p1 = p;
  603. X
  604. X          PATPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  605. X          /* Clear the whole map */
  606. X          bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  607. X          /* Read in characters and ranges, setting map bits */
  608. X          while (1)
  609. X            {
  610. X              PATFETCH (c);
  611. X              if (c == ']' && p != p1 + 1) break;
  612. X              if (*p == '-' && p[1] != ']')
  613. X                {
  614. X                  PATFETCH (c1);
  615. X                  PATFETCH (c1);
  616. X                  while (c <= c1)
  617. X                    b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH), c++;
  618. X                }
  619. X              else
  620. X                {
  621. X                  b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH);
  622. X                }
  623. X            }
  624. X          /* Discard any bitmap bytes that are all 0 at the end of the map.
  625. X             Decrement the map-length byte too. */
  626. X          while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
  627. X            b[-1]--;
  628. X          b += b[-1];
  629. X          break;
  630. X
  631. X        case '(':
  632. X          if (! (obscure_syntax & RE_NO_BK_PARENS))
  633. X            goto normal_char;
  634. X          else
  635. X            goto handle_open;
  636. X
  637. X        case ')':
  638. X          if (! (obscure_syntax & RE_NO_BK_PARENS))
  639. X            goto normal_char;
  640. X          else
  641. X            goto handle_close;
  642. X
  643. X        case '\n':
  644. X          if (! (obscure_syntax & RE_NEWLINE_OR))
  645. X            goto normal_char;
  646. X          else
  647. X            goto handle_bar;
  648. X
  649. X        case '|':
  650. X          if (! (obscure_syntax & RE_NO_BK_VBAR))
  651. X            goto normal_char;
  652. X          else
  653. X            goto handle_bar;
  654. X
  655. X        case '\\':
  656. X          if (p == pend) goto invalid_pattern;
  657. X          PATFETCH_RAW (c);
  658. X          switch (c)
  659. X            {
  660. X            case '(':
  661. X              if (obscure_syntax & RE_NO_BK_PARENS)
  662. X                goto normal_backsl;
  663. X            handle_open:
  664. X              if (stackp == stacke) goto nesting_too_deep;
  665. X              if (regnum < RE_NREGS)
  666. X                {
  667. X                  PATPUSH (start_memory);
  668. X                  PATPUSH (regnum);
  669. X                }
  670. X              *stackp++ = b - bufp->buffer;
  671. X              *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  672. X              *stackp++ = regnum++;
  673. X              *stackp++ = begalt - bufp->buffer;
  674. X              fixup_jump = 0;
  675. X              laststart = 0;
  676. X              begalt = b;
  677. X              break;
  678. X
  679. X            case ')':
  680. X              if (obscure_syntax & RE_NO_BK_PARENS)
  681. X                goto normal_backsl;
  682. X            handle_close:
  683. X              if (stackp == stackb) goto unmatched_close;
  684. X              begalt = *--stackp + bufp->buffer;
  685. X              if (fixup_jump)
  686. X                store_jump (fixup_jump, jump, b);
  687. X              if (stackp[-1] < RE_NREGS)
  688. X                {
  689. X                  PATPUSH (stop_memory);
  690. X                  PATPUSH (stackp[-1]);
  691. X                }
  692. X              stackp -= 2;
  693. X              fixup_jump = 0;
  694. X              if (*stackp)
  695. X                fixup_jump = *stackp + bufp->buffer - 1;
  696. X              laststart = *--stackp + bufp->buffer;
  697. X              break;
  698. X
  699. X            case '|':
  700. X              if (obscure_syntax & RE_NO_BK_VBAR)
  701. X                goto normal_backsl;
  702. X            handle_bar:
  703. X              insert_jump (on_failure_jump, begalt, b + 6, b);
  704. X              pending_exact = 0;
  705. X              b += 3;
  706. X              if (fixup_jump)
  707. X                store_jump (fixup_jump, jump, b);
  708. X              fixup_jump = b;
  709. X              b += 3;
  710. X              laststart = 0;
  711. X              begalt = b;
  712. X              break;
  713. X
  714. X#ifdef emacs
  715. X            case '=':
  716. X              PATPUSH (at_dot);
  717. X              break;
  718. X
  719. X            case 's':
  720. X              laststart = b;
  721. X              PATPUSH (syntaxspec);
  722. X              PATFETCH (c);
  723. X              PATPUSH (syntax_spec_code[c]);
  724. X              break;
  725. X
  726. X            case 'S':
  727. X              laststart = b;
  728. X              PATPUSH (notsyntaxspec);
  729. X              PATFETCH (c);
  730. X              PATPUSH (syntax_spec_code[c]);
  731. X              break;
  732. X#endif /* emacs */
  733. X
  734. X            case 'w':
  735. X              laststart = b;
  736. X              PATPUSH (wordchar);
  737. X              break;
  738. X
  739. X            case 'W':
  740. X              laststart = b;
  741. X              PATPUSH (notwordchar);
  742. X              break;
  743. X
  744. X            case '<':
  745. X              PATPUSH (wordbeg);
  746. X              break;
  747. X
  748. X            case '>':
  749. X              PATPUSH (wordend);
  750. X              break;
  751. X
  752. X            case 'b':
  753. X              PATPUSH (wordbound);
  754. X              break;
  755. X
  756. X            case 'B':
  757. X              PATPUSH (notwordbound);
  758. X              break;
  759. X
  760. X            case '`':
  761. X              PATPUSH (begbuf);
  762. X              break;
  763. X
  764. X            case '\'':
  765. X              PATPUSH (endbuf);
  766. X              break;
  767. X
  768. X            case '1':
  769. X            case '2':
  770. X            case '3':
  771. X            case '4':
  772. X            case '5':
  773. X            case '6':
  774. X            case '7':
  775. X            case '8':
  776. X            case '9':
  777. X              c1 = c - '0';
  778. X              if (c1 >= regnum)
  779. X                goto normal_char;
  780. X              for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  781. X                if (*stackt == c1)
  782. X                  goto normal_char;
  783. X              laststart = b;
  784. X              PATPUSH (duplicate);
  785. X              PATPUSH (c1);
  786. X              break;
  787. X
  788. X            case '+':
  789. X            case '?':
  790. X              if (obscure_syntax & RE_BK_PLUS_QM)
  791. X                goto handle_plus;
  792. X
  793. X            default:
  794. X            normal_backsl:
  795. X              /* You might think it would be useful for \ to mean
  796. X                 not to translate; but if we don't translate it
  797. X                 it will never match anything.  */
  798. X              if (translate) c = translate[c];
  799. X              goto normal_char;
  800. X            }
  801. X          break;
  802. X
  803. X        default:
  804. X        normal_char:
  805. X          if (!pending_exact || pending_exact + *pending_exact + 1 != b
  806. X              || *pending_exact == 0177 || *p == '*' || *p == '^'
  807. X              || ((obscure_syntax & RE_BK_PLUS_QM)
  808. X                  ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  809. X                  : (*p == '+' || *p == '?')))
  810. X            {
  811. X              laststart = b;
  812. X              PATPUSH (exactn);
  813. X              pending_exact = b;
  814. X              PATPUSH (0);
  815. X            }
  816. X          PATPUSH (c);
  817. X          (*pending_exact)++;
  818. X        }
  819. X    }
  820. X
  821. X  if (fixup_jump)
  822. X    store_jump (fixup_jump, jump, b);
  823. X
  824. X  if (stackp != stackb) goto unmatched_open;
  825. X
  826. X  bufp->used = b - bufp->buffer;
  827. X  return 0;
  828. X
  829. X invalid_pattern:
  830. X  return "Invalid regular expression";
  831. X
  832. X unmatched_open:
  833. X  return "Unmatched \\(";
  834. X
  835. X unmatched_close:
  836. X  return "Unmatched \\)";
  837. X
  838. X end_of_pattern:
  839. X  return "Premature end of regular expression";
  840. X
  841. X nesting_too_deep:
  842. X  return "Nesting too deep";
  843. X
  844. X too_big:
  845. X  return "Regular expression too big";
  846. X
  847. X memory_exhausted:
  848. X  return "Memory exhausted";
  849. X}
  850. X
  851. X/* Store where `from' points a jump operation to jump to where `to' points.
  852. X  `opcode' is the opcode to store. */
  853. X
  854. Xstatic int
  855. Xstore_jump (from, opcode, to)
  856. X     char *from, *to;
  857. X     char opcode;
  858. X{
  859. X  from[0] = opcode;
  860. X  from[1] = (to - (from + 3)) & 0377;
  861. X  from[2] = (to - (from + 3)) >> 8;
  862. X}
  863. X
  864. X/* Open up space at char FROM, and insert there a jump to TO.
  865. X   CURRENT_END gives te end of the storage no in use,
  866. X   so we know how much data to copy up.
  867. X   OP is the opcode of the jump to insert.
  868. X
  869. X   If you call this function, you must zero out pending_exact.  */
  870. X
  871. Xstatic int
  872. Xinsert_jump (op, from, to, current_end)
  873. X     char op;
  874. X     char *from, *to, *current_end;
  875. X{
  876. X  register char *pto = current_end + 3;
  877. X  register char *pfrom = current_end;
  878. X  while (pfrom != from)
  879. X    *--pto = *--pfrom;
  880. X  store_jump (from, op, to);
  881. X}
  882. X
  883. X/* Given a pattern, compute a fastmap from it.
  884. X The fastmap records which of the (1 << BYTEWIDTH) possible characters
  885. X can start a string that matches the pattern.
  886. X This fastmap is used by re_search to skip quickly over totally implausible text.
  887. X
  888. X The caller must supply the address of a (1 << BYTEWIDTH)-byte data area
  889. X as bufp->fastmap.
  890. X The other components of bufp describe the pattern to be used.  */
  891. X
  892. Xvoid
  893. Xre_compile_fastmap (bufp)
  894. X     struct re_pattern_buffer *bufp;
  895. X{
  896. X  unsigned char *pattern = (unsigned char *) bufp->buffer;
  897. X  int size = bufp->used;
  898. X  register char *fastmap = bufp->fastmap;
  899. X  register unsigned char *p = pattern;
  900. X  register unsigned char *pend = pattern + size;
  901. X  register int j, k;
  902. X  unsigned char *translate = (unsigned char *) bufp->translate;
  903. X
  904. X  unsigned char *stackb[NFAILURES];
  905. X  unsigned char **stackp = stackb;
  906. X
  907. X  bzero (fastmap, (1 << BYTEWIDTH));
  908. X  bufp->fastmap_accurate = 1;
  909. X  bufp->can_be_null = 0;
  910. X
  911. X  while (p)
  912. X    {
  913. X      if (p == pend)
  914. X        {
  915. X          bufp->can_be_null = 1;
  916. X          break;
  917. X        }
  918. X#ifdef SWITCH_ENUM_BUG
  919. X      switch ((int) ((enum regexpcode) *p++))
  920. X#else
  921. X      switch ((enum regexpcode) *p++)
  922. X#endif
  923. X        {
  924. X        case exactn:
  925. X          if (translate)
  926. X            fastmap[translate[p[1]]] = 1;
  927. X          else
  928. X            fastmap[p[1]] = 1;
  929. X          break;
  930. X
  931. X        case begline:
  932. X        case before_dot:
  933. X        case at_dot:
  934. X        case after_dot:
  935. X        case begbuf:
  936. X        case endbuf:
  937. X        case wordbound:
  938. X        case notwordbound:
  939. X        case wordbeg:
  940. X        case wordend:
  941. X          continue;
  942. X
  943. X        case endline:
  944. X          if (translate)
  945. X            fastmap[translate['\n']] = 1;
  946. X          else
  947. X            fastmap['\n'] = 1;
  948. X          if (bufp->can_be_null != 1)
  949. X            bufp->can_be_null = 2;
  950. X          break;
  951. X
  952. X        case finalize_jump:
  953. X        case maybe_finalize_jump:
  954. X        case jump:
  955. X        case dummy_failure_jump:
  956. X          bufp->can_be_null = 1;
  957. X          j = *p++ & 0377;
  958. X          j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  959. X          p += j + 1;           /* The 1 compensates for missing ++ above */
  960. X          if (j > 0)
  961. X            continue;
  962. X          /* Jump backward reached implies we just went through
  963. X             the body of a loop and matched nothing.
  964. X             Opcode jumped to should be an on_failure_jump.
  965. X             Just treat it like an ordinary jump.
  966. X             For a * loop, it has pushed its failure point already;
  967. X             if so, discard that as redundant.  */
  968. X          if ((enum regexpcode) *p != on_failure_jump)
  969. X            continue;
  970. X          p++;
  971. X          j = *p++ & 0377;
  972. X          j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  973. X          p += j + 1;           /* The 1 compensates for missing ++ above */
  974. X          if (stackp != stackb && *stackp == p)
  975. X            stackp--;
  976. X          continue;
  977. X
  978. X        case on_failure_jump:
  979. X          j = *p++ & 0377;
  980. X          j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  981. X          p++;
  982. X          *++stackp = p + j;
  983. X          continue;
  984. X
  985. X        case start_memory:
  986. X        case stop_memory:
  987. X          p++;
  988. X          continue;
  989. X
  990. X        case duplicate:
  991. X          bufp->can_be_null = 1;
  992. X          fastmap['\n'] = 1;
  993. X        case anychar:
  994. X          for (j = 0; j < (1 << BYTEWIDTH); j++)
  995. X            if (j != '\n')
  996. X              fastmap[j] = 1;
  997. X          if (bufp->can_be_null)
  998. X            return;
  999. X          /* Don't return; check the alternative paths
  1000. X             so we can set can_be_null if appropriate.  */
  1001. X          break;
  1002. X
  1003. X        case wordchar:
  1004. X          for (j = 0; j < (1 << BYTEWIDTH); j++)
  1005. X            if (SYNTAX (j) == Sword)
  1006. X              fastmap[j] = 1;
  1007. X          break;
  1008. X
  1009. X        case notwordchar:
  1010. X          for (j = 0; j < (1 << BYTEWIDTH); j++)
  1011. X            if (SYNTAX (j) != Sword)
  1012. X              fastmap[j] = 1;
  1013. X          break;
  1014. X
  1015. X#ifdef emacs
  1016. X        case syntaxspec:
  1017. X          k = *p++;
  1018. X          for (j = 0; j < (1 << BYTEWIDTH); j++)
  1019. X            if (SYNTAX (j) == (enum syntaxcode) k)
  1020. X              fastmap[j] = 1;
  1021. X          break;
  1022. X
  1023. X        case notsyntaxspec:
  1024. X          k = *p++;
  1025. X          for (j = 0; j < (1 << BYTEWIDTH); j++)
  1026. X            if (SYNTAX (j) != (enum syntaxcode) k)
  1027. X              fastmap[j] = 1;
  1028. X          break;
  1029. X#endif /* emacs */
  1030. X
  1031. X        case charset:
  1032. X          for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1033. X            if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  1034. X              {
  1035. X                if (translate)
  1036. X                  fastmap[translate[j]] = 1;
  1037. X                else
  1038. X                  fastmap[j] = 1;
  1039. X              }
  1040. X          break;
  1041. X
  1042. X        case charset_not:
  1043. X          /* Chars beyond end of map must be allowed */
  1044. X          for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  1045. X            if (translate)
  1046. X              fastmap[translate[j]] = 1;
  1047. X            else
  1048. X              fastmap[j] = 1;
  1049. X
  1050. X          for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1051. X            if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  1052. X              {
  1053. X                if (translate)
  1054. X                  fastmap[translate[j]] = 1;
  1055. X                else
  1056. X                  fastmap[j] = 1;
  1057. X              }
  1058. X          break;
  1059. X        }
  1060. X
  1061. X      /* Get here means we have successfully found the possible starting characters
  1062. X         of one path of the pattern.  We need not follow this path any farther.
  1063. X         Instead, look at the next alternative remembered in the stack. */
  1064. X      if (stackp != stackb)
  1065. X        p = *stackp--;
  1066. X      else
  1067. X        break;
  1068. X    }
  1069. X}
  1070. X
  1071. X/* Like re_search_2, below, but only one string is specified. */
  1072. X
  1073. Xint
  1074. Xre_search (pbufp, string, size, startpos, range, regs)
  1075. X     struct re_pattern_buffer *pbufp;
  1076. X     char *string;
  1077. X     int size, startpos, range;
  1078. X     struct re_registers *regs;
  1079. X{
  1080. X  return re_search_2 (pbufp, 0, 0, string, size, startpos, range, regs, size);
  1081. X}
  1082. X
  1083. X/* Like re_match_2 but tries first a match starting at index STARTPOS,
  1084. X   then at STARTPOS + 1, and so on.
  1085. X   RANGE is the number of places to try before giving up.
  1086. X   If RANGE is negative, the starting positions tried are
  1087. X    STARTPOS, STARTPOS - 1, etc.
  1088. X   It is up to the caller to make sure that range is not so large
  1089. X   as to take the starting position outside of the input strings.
  1090. X
  1091. XThe value returned is the position at which the match was found,
  1092. X or -1 if no match was found,
  1093. X or -2 if error (such as failure stack overflow).  */
  1094. X
  1095. Xint
  1096. Xre_search_2 (pbufp, string1, size1, string2, size2, startpos, range, regs, mstop)
  1097. X     struct re_pattern_buffer *pbufp;
  1098. X     char *string1, *string2;
  1099. X     int size1, size2;
  1100. X     int startpos;
  1101. X     register int range;
  1102. X     struct re_registers *regs;
  1103. X     int mstop;
  1104. X{
  1105. X  register char *fastmap = pbufp->fastmap;
  1106. X  register unsigned char *translate = (unsigned char *) pbufp->translate;
  1107. X  int total = size1 + size2;
  1108. X  int val;
  1109. X
  1110. X  /* Update the fastmap now if not correct already */
  1111. X  if (fastmap && !pbufp->fastmap_accurate)
  1112. X    re_compile_fastmap (pbufp);
  1113. X
  1114. X  /* Don't waste time in a long search for a pattern
  1115. X     that says it is anchored.  */
  1116. X  if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
  1117. X      && range > 0)
  1118. X    {
  1119. X      if (startpos > 0)
  1120. X        return -1;
  1121. X      else
  1122. X        range = 1;
  1123. X    }
  1124. X
  1125. X  while (1)
  1126. X    {
  1127. X      /* If a fastmap is supplied, skip quickly over characters
  1128. X         that cannot possibly be the start of a match.
  1129. X         Note, however, that if the pattern can possibly match
  1130. X         the null string, we must test it at each starting point
  1131. X         so that we take the first null string we get.  */
  1132. X
  1133. X      if (fastmap && startpos < total && pbufp->can_be_null != 1)
  1134. X        {
  1135. X          if (range > 0)
  1136. X            {
  1137. X              register int lim = 0;
  1138. X              register unsigned char *p;
  1139. X              int irange = range;
  1140. X              if (startpos < size1 && startpos + range >= size1)
  1141. X                lim = range - (size1 - startpos);
  1142. X
  1143. X              p = ((unsigned char *)
  1144. X                   &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
  1145. X
  1146. X              if (translate)
  1147. X                {
  1148. X                  while (range > lim && !fastmap[translate[*p++]])
  1149. X                    range--;
  1150. X                }
  1151. X              else
  1152. X                {
  1153. X                  while (range > lim && !fastmap[*p++])
  1154. X                    range--;
  1155. X                }
  1156. X              startpos += irange - range;
  1157. X            }
  1158. X          else
  1159. X            {
  1160. X              register unsigned char c;
  1161. X              if (startpos >= size1)
  1162. X                c = string2[startpos - size1];
  1163. X              else
  1164. X                c = string1[startpos];
  1165. X              c &= 0xff;
  1166. X              if (translate ? !fastmap[translate[c]] : !fastmap[c])
  1167. X                goto advance;
  1168. X            }
  1169. X        }
  1170. X
  1171. X      if (range >= 0 && startpos == total
  1172. X          && fastmap && pbufp->can_be_null == 0)
  1173. X        return -1;
  1174. X
  1175. X      val = re_match_2 (pbufp, string1, size1, string2, size2, startpos, regs, mstop);
  1176. X      if (0 <= val)
  1177. X        {
  1178. X          if (val == -2)
  1179. X            return -2;
  1180. X          return startpos;
  1181. X        }
  1182. X
  1183. X#ifdef C_ALLOCA
  1184. X      alloca (0);
  1185. X#endif /* C_ALLOCA */
  1186. X
  1187. X    advance:
  1188. X      if (!range) break;
  1189. X      if (range > 0) range--, startpos++; else range++, startpos--;
  1190. X    }
  1191. X  return -1;
  1192. X}
  1193. X
  1194. X#ifndef emacs   /* emacs never uses this */
  1195. Xint
  1196. Xre_match (pbufp, string, size, pos, regs)
  1197. X     struct re_pattern_buffer *pbufp;
  1198. X     char *string;
  1199. X     int size, pos;
  1200. X     struct re_registers *regs;
  1201. X{
  1202. X  return re_match_2 (pbufp, 0, 0, string, size, pos, regs, size);
  1203. X}
  1204. X#endif /* emacs */
  1205. X
  1206. X/* Maximum size of failure stack.  Beyond this, overflow is an error.  */
  1207. X
  1208. Xint re_max_failures = 2000;
  1209. X
  1210. Xstatic int bcmp_translate();
  1211. X/* Match the pattern described by PBUFP
  1212. X   against data which is the virtual concatenation of STRING1 and STRING2.
  1213. X   SIZE1 and SIZE2 are the sizes of the two data strings.
  1214. X   Start the match at position POS.
  1215. X   Do not consider matching past the position MSTOP.
  1216. X
  1217. X   If pbufp->fastmap is nonzero, then it had better be up to date.
  1218. X
  1219. X   The reason that the data to match are specified as two components
  1220. X   which are to be regarded as concatenated
  1221. X   is so this function can be used directly on the contents of an Emacs buffer.
  1222. X
  1223. X   -1 is returned if there is no match.  -2 is returned if there is
  1224. X   an error (such as match stack overflow).  Otherwise the value is the length
  1225. X   of the substring which was matched.  */
  1226. X
  1227. Xint
  1228. Xre_match_2 (pbufp, string1, size1, string2, size2, pos, regs, mstop)
  1229. X     struct re_pattern_buffer *pbufp;
  1230. X     unsigned char *string1, *string2;
  1231. X     int size1, size2;
  1232. X     int pos;
  1233. X     struct re_registers *regs;
  1234. X     int mstop;
  1235. X{
  1236. X  register unsigned char *p = (unsigned char *) pbufp->buffer;
  1237. X  register unsigned char *pend = p + pbufp->used;
  1238. X  /* End of first string */
  1239. X  unsigned char *end1;
  1240. X  /* End of second string */
  1241. X  unsigned char *end2;
  1242. X  /* Pointer just past last char to consider matching */
  1243. X  unsigned char *end_match_1, *end_match_2;
  1244. X  register unsigned char *d, *dend;
  1245. X  register int mcnt;
  1246. X  unsigned char *translate = (unsigned char *) pbufp->translate;
  1247. X
  1248. X /* Failure point stack.  Each place that can handle a failure further down the line
  1249. X    pushes a failure point on this stack.  It consists of two char *'s.
  1250. X    The first one pushed is where to resume scanning the pattern;
  1251. X    the second pushed is where to resume scanning the strings.
  1252. X    If the latter is zero, the failure point is a "dummy".
  1253. X    If a failure happens and the innermost failure point is dormant,
  1254. X    it discards that failure point and tries the next one. */
  1255. X
  1256. X  unsigned char *initial_stack[2 * NFAILURES];
  1257. X  unsigned char **stackb = initial_stack;
  1258. X  unsigned char **stackp = stackb, **stacke = &stackb[2 * NFAILURES];
  1259. X
  1260. X  /* Information on the "contents" of registers.
  1261. X     These are pointers into the input strings; they record
  1262. X     just what was matched (on this attempt) by some part of the pattern.
  1263. X     The start_memory command stores the start of a register's contents
  1264. X     and the stop_memory command stores the end.
  1265. X
  1266. X     At that point, regstart[regnum] points to the first character in the register,
  1267. X     regend[regnum] points to the first character beyond the end of the register,
  1268. X     regstart_seg1[regnum] is true iff regstart[regnum] points into string1,
  1269. X     and regend_seg1[regnum] is true iff regend[regnum] points into string1.  */
  1270. X
  1271. X  unsigned char *regstart[RE_NREGS];
  1272. X  unsigned char *regend[RE_NREGS];
  1273. X  unsigned char regstart_seg1[RE_NREGS], regend_seg1[RE_NREGS];
  1274. X
  1275. X  /* Set up pointers to ends of strings.
  1276. X     Don't allow the second string to be empty unless both are empty.  */
  1277. X  if (!size2)
  1278. X    {
  1279. X      string2 = string1;
  1280. X      size2 = size1;
  1281. X      string1 = 0;
  1282. X      size1 = 0;
  1283. X    }
  1284. X  end1 = string1 + size1;
  1285. X  end2 = string2 + size2;
  1286. X
  1287. X  /* Compute where to stop matching, within the two strings */
  1288. X  if (mstop <= size1)
  1289. X    {
  1290. X      end_match_1 = string1 + mstop;
  1291. X      end_match_2 = string2;
  1292. X    }
  1293. X  else
  1294. X    {
  1295. X      end_match_1 = end1;
  1296. X      end_match_2 = string2 + mstop - size1;
  1297. X    }
  1298. X
  1299. X  /* Initialize \) text positions to -1
  1300. X     to mark ones that no \( or \) has been seen for.  */
  1301. X
  1302. X  for (mcnt = 0; mcnt < sizeof (regend) / sizeof (*regend); mcnt++)
  1303. X    regend[mcnt] = (unsigned char *) -1;
  1304. X
  1305. X  /* `p' scans through the pattern as `d' scans through the data.
  1306. X     `dend' is the end of the input string that `d' points within.
  1307. X     `d' is advanced into the following input string whenever necessary,
  1308. X     but this happens before fetching;
  1309. X     therefore, at the beginning of the loop,
  1310. X     `d' can be pointing at the end of a string,
  1311. X     but it cannot equal string2.  */
  1312. X
  1313. X  if (pos <= size1)
  1314. X    d = string1 + pos, dend = end_match_1;
  1315. X  else
  1316. X    d = string2 + pos - size1, dend = end_match_2;
  1317. X
  1318. X/* Write PREFETCH; just before fetching a character with *d.  */
  1319. X#define PREFETCH \
  1320. X while (d == dend)                                                  \
  1321. X  { if (dend == end_match_2) goto fail;  /* end of string2 => failure */   \
  1322. X    d = string2;  /* end of string1 => advance to string2. */       \
  1323. X    dend = end_match_2; }
  1324. X
  1325. X  /* This loop loops over pattern commands.
  1326. X     It exits by returning from the function if match is complete,
  1327. X     or it drops through if match fails at this starting point in the input data. */
  1328. X
  1329. X  while (1)
  1330. X    {
  1331. X      if (p == pend)
  1332. X        /* End of pattern means we have succeeded! */
  1333. X        {
  1334. X          /* If caller wants register contents data back, convert it to indices */
  1335. X          if (regs)
  1336. X            {
  1337. X              regs->start[0] = pos;
  1338. X              if (dend == end_match_1)
  1339. X                regs->end[0] = d - string1;
  1340. X              else
  1341. X                regs->end[0] = d - string2 + size1;
  1342. X              for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  1343. X                {
  1344. X                  if (regend[mcnt] == (unsigned char *) -1)
  1345. X                    {
  1346. X                      regs->start[mcnt] = -1;
  1347. X                      regs->end[mcnt] = -1;
  1348. X                      continue;
  1349. X                    }
  1350. X                  if (regstart_seg1[mcnt])
  1351. X                    regs->start[mcnt] = regstart[mcnt] - string1;
  1352. X                  else
  1353. X                    regs->start[mcnt] = regstart[mcnt] - string2 + size1;
  1354. X                  if (regend_seg1[mcnt])
  1355. X                    regs->end[mcnt] = regend[mcnt] - string1;
  1356. X                  else
  1357. X                    regs->end[mcnt] = regend[mcnt] - string2 + size1;
  1358. X                }
  1359. X            }
  1360. X          if (dend == end_match_1)
  1361. X            return (d - string1 - pos);
  1362. X          else
  1363. X            return d - string2 + size1 - pos;
  1364. X        }
  1365. X
  1366. X      /* Otherwise match next pattern command */
  1367. X#ifdef SWITCH_ENUM_BUG
  1368. X      switch ((int) ((enum regexpcode) *p++))
  1369. X#else
  1370. X      switch ((enum regexpcode) *p++)
  1371. X#endif
  1372. X        {
  1373. X
  1374. X        /* \( is represented by a start_memory, \) by a stop_memory.
  1375. X            Both of those commands contain a "register number" argument.
  1376. X            The text matched within the \( and \) is recorded under that number.
  1377. X            Then, \<digit> turns into a `duplicate' command which
  1378. X            is followed by the numeric value of <digit> as the register number. */
  1379. X
  1380. X        case start_memory:
  1381. X          regstart[*p] = d;
  1382. X          regstart_seg1[*p++] = (dend == end_match_1);
  1383. X          break;
  1384. X
  1385. X        case stop_memory:
  1386. X          regend[*p] = d;
  1387. X          regend_seg1[*p++] = (dend == end_match_1);
  1388. X          break;
  1389. X
  1390. X        case duplicate:
  1391. X          {
  1392. X            int regno = *p++;   /* Get which register to match against */
  1393. X            register unsigned char *d2, *dend2;
  1394. X
  1395. X            d2 = regstart[regno];
  1396. X            dend2 = ((regstart_seg1[regno] == regend_seg1[regno])
  1397. X                     ? regend[regno] : end_match_1);
  1398. X            while (1)
  1399. X              {
  1400. X                /* Advance to next segment in register contents, if necessary */
  1401. X                while (d2 == dend2)
  1402. X                  {
  1403. X                    if (dend2 == end_match_2) break;
  1404. X                    if (dend2 == regend[regno]) break;
  1405. X                    d2 = string2, dend2 = regend[regno];  /* end of string1 => advance to string2. */
  1406. X                  }
  1407. X                /* At end of register contents => success */
  1408. X                if (d2 == dend2) break;
  1409. X
  1410. X                /* Advance to next segment in data being matched, if necessary */
  1411. X                PREFETCH;
  1412. X
  1413. X                /* mcnt gets # consecutive chars to compare */
  1414. X                mcnt = dend - d;
  1415. X                if (mcnt > dend2 - d2)
  1416. X                  mcnt = dend2 - d2;
  1417. X                /* Compare that many; failure if mismatch, else skip them. */
  1418. X                if (translate ? bcmp_translate (d, d2, mcnt, translate) : bcmp (d, d2, mcnt))
  1419. X                  goto fail;
  1420. X                d += mcnt, d2 += mcnt;
  1421. X              }
  1422. X          }
  1423. X          break;
  1424. X
  1425. X        case anychar:
  1426. X          /* fetch a data character */
  1427. X          PREFETCH;
  1428. X          /* Match anything but a newline.  */
  1429. X          if ((translate ? translate[*d++] : *d++) == '\n')
  1430. X            goto fail;
  1431. X          break;
  1432. X
  1433. X        case charset:
  1434. X        case charset_not:
  1435. X          {
  1436. X            /* Nonzero for charset_not */
  1437. X            int not = 0;
  1438. X            register int c;
  1439. X            if (*(p - 1) == (unsigned char) charset_not)
  1440. X              not = 1;
  1441. X
  1442. X            /* fetch a data character */
  1443. X            PREFETCH;
  1444. X
  1445. X            if (translate)
  1446. X              c = translate [*d];
  1447. X            else
  1448. X              c = *d;
  1449. X
  1450. X            if (c < *p * BYTEWIDTH
  1451. X                && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1452. X              not = !not;
  1453. X
  1454. X            p += 1 + *p;
  1455. X
  1456. X            if (!not) goto fail;
  1457. X            d++;
  1458. X            break;
  1459. X          }
  1460. X
  1461. X        case begline:
  1462. X          if (d == string1 || d[-1] == '\n')
  1463. X            break;
  1464. X          goto fail;
  1465. X
  1466. X        case endline:
  1467. X          if (d == end2
  1468. X              || (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
  1469. X            break;
  1470. X          goto fail;
  1471. X
  1472. X        /* "or" constructs ("|") are handled by starting each alternative
  1473. X            with an on_failure_jump that points to the start of the next alternative.
  1474. X            Each alternative except the last ends with a jump to the joining point.
  1475. X            (Actually, each jump except for the last one really jumps
  1476. X             to the following jump, because tensioning the jumps is a hassle.) */
  1477. X
  1478. X        /* The start of a stupid repeat has an on_failure_jump that points
  1479. X           past the end of the repeat text.
  1480. X           This makes a failure point so that, on failure to match a repetition,
  1481. X           matching restarts past as many repetitions have been found
  1482. X           with no way to fail and look for another one.  */
  1483. X
  1484. X        /* A smart repeat is similar but loops back to the on_failure_jump
  1485. X           so that each repetition makes another failure point. */
  1486. X
  1487. X        case on_failure_jump:
  1488. X          if (stackp == stacke)
  1489. X            {
  1490. X              unsigned char **stackx;
  1491. X              if (stacke - stackb > re_max_failures * 2)
  1492. X                return -2;
  1493. X              stackx = (unsigned char **) alloca (2 * (stacke - stackb)
  1494. X                                         * sizeof (char *));
  1495. X              bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  1496. X              stackp = stackx + (stackp - stackb);
  1497. X              stacke = stackx + 2 * (stacke - stackb);
  1498. X              stackb = stackx;
  1499. X            }
  1500. X          mcnt = *p++ & 0377;
  1501. X          mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1502. X          p++;
  1503. X          *stackp++ = mcnt + p;
  1504. X          *stackp++ = d;
  1505. X          break;
  1506. X
  1507. X        /* The end of a smart repeat has an maybe_finalize_jump back.
  1508. X           Change it either to a finalize_jump or an ordinary jump. */
  1509. X
  1510. X        case maybe_finalize_jump:
  1511. X          mcnt = *p++ & 0377;
  1512. X          mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1513. X          p++;
  1514. X          {
  1515. X            register unsigned char *p2 = p;
  1516. X            /* Compare what follows with the begining of the repeat.
  1517. X               If we can establish that there is nothing that they would
  1518. X               both match, we can change to finalize_jump */
  1519. X            while (p2 != pend
  1520. X                   && (*p2 == (unsigned char) stop_memory
  1521. X                       || *p2 == (unsigned char) start_memory))
  1522. X              p2++;
  1523. X            if (p2 == pend)
  1524. X              p[-3] = (unsigned char) finalize_jump;
  1525. X            else if (*p2 == (unsigned char) exactn
  1526. X                     || *p2 == (unsigned char) endline)
  1527. X              {
  1528. X                register int c = *p2 == (unsigned char) endline ? '\n' : p2[2];
  1529. X                register unsigned char *p1 = p + mcnt;
  1530. X                /* p1[0] ... p1[2] are an on_failure_jump.
  1531. X                   Examine what follows that */
  1532. X                if (p1[3] == (unsigned char) exactn && p1[5] != c)
  1533. X                  p[-3] = (unsigned char) finalize_jump;
  1534. X                else if (p1[3] == (unsigned char) charset
  1535. X                         || p1[3] == (unsigned char) charset_not)
  1536. X                  {
  1537. X                    int not = p1[3] == (unsigned char) charset_not;
  1538. X                    if (c < p1[4] * BYTEWIDTH
  1539. X                        && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1540. X                      not = !not;
  1541. X                    /* not is 1 if c would match */
  1542. X                    /* That means it is not safe to finalize */
  1543. X                    if (!not)
  1544. X                      p[-3] = (unsigned char) finalize_jump;
  1545. X                  }
  1546. X              }
  1547. X          }
  1548. X          p -= 2;
  1549. X          if (p[-1] != (unsigned char) finalize_jump)
  1550. X            {
  1551. X              p[-1] = (unsigned char) jump;
  1552. X              goto nofinalize;
  1553. X            }
  1554. X
  1555. X        /* The end of a stupid repeat has a finalize-jump
  1556. X           back to the start, where another failure point will be made
  1557. X           which will point after all the repetitions found so far. */
  1558. X
  1559. X        case finalize_jump:
  1560. X          stackp -= 2;
  1561. X
  1562. X        case jump:
  1563. X        nofinalize:
  1564. X          mcnt = *p++ & 0377;
  1565. X          mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1566. X          p += mcnt + 1;        /* The 1 compensates for missing ++ above */
  1567. X          break;
  1568. X
  1569. X        case dummy_failure_jump:
  1570. X          if (stackp == stacke)
  1571. X            {
  1572. X              unsigned char **stackx
  1573. X                = (unsigned char **) alloca (2 * (stacke - stackb)
  1574. X                                             * sizeof (char *));
  1575. X              bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  1576. X              stackp = stackx + (stackp - stackb);
  1577. X              stacke = stackx + 2 * (stacke - stackb);
  1578. X              stackb = stackx;
  1579. X            }
  1580. X          *stackp++ = 0;
  1581. X          *stackp++ = 0;
  1582. X          goto nofinalize;
  1583. X
  1584. X        case wordbound:
  1585. X          if (d == string1  /* Points to first char */
  1586. X              || d == end2  /* Points to end */
  1587. X              || (d == end1 && size2 == 0)) /* Points to end */
  1588. X            break;
  1589. X          if ((SYNTAX (d[-1]) == Sword)
  1590. X              != (SYNTAX (d == end1 ? *string2 : *d) == Sword))
  1591. X            break;
  1592. X          goto fail;
  1593. X
  1594. X        case notwordbound:
  1595. X          if (d == string1  /* Points to first char */
  1596. X              || d == end2  /* Points to end */
  1597. X              || (d == end1 && size2 == 0)) /* Points to end */
  1598. X            goto fail;
  1599. X          if ((SYNTAX (d[-1]) == Sword)
  1600. X              != (SYNTAX (d == end1 ? *string2 : *d) == Sword))
  1601. X            goto fail;
  1602. X          break;
  1603. X
  1604. X        case wordbeg:
  1605. X          if (d == end2  /* Points to end */
  1606. X              || (d == end1 && size2 == 0) /* Points to end */
  1607. X              || SYNTAX (* (d == end1 ? string2 : d)) != Sword) /* Next char not a letter */
  1608. X            goto fail;
  1609. X          if (d == string1  /* Points to first char */
  1610. X              || SYNTAX (d[-1]) != Sword)  /* prev char not letter */
  1611. X            break;
  1612. X          goto fail;
  1613. X
  1614. X        case wordend:
  1615. X          if (d == string1  /* Points to first char */
  1616. X              || SYNTAX (d[-1]) != Sword)  /* prev char not letter */
  1617. X            goto fail;
  1618. X          if (d == end2  /* Points to end */
  1619. X              || (d == end1 && size2 == 0) /* Points to end */
  1620. X              || SYNTAX (d == end1 ? *string2 : *d) != Sword) /* Next char not a letter */
  1621. X            break;
  1622. X          goto fail;
  1623. X
  1624. X#ifdef emacs
  1625. X        case before_dot:
  1626. X          if (((d - string2 <= (unsigned) size2)
  1627. X               ? d - bf_p2 : d - bf_p1)
  1628. X              <= point)
  1629. X            goto fail;
  1630. X          break;
  1631. X
  1632. X        case at_dot:
  1633. X          if (((d - string2 <= (unsigned) size2)
  1634. X               ? d - bf_p2 : d - bf_p1)
  1635. X              == point)
  1636. X            goto fail;
  1637. X          break;
  1638. X
  1639. X        case after_dot:
  1640. X          if (((d - string2 <= (unsigned) size2)
  1641. X               ? d - bf_p2 : d - bf_p1)
  1642. X              >= point)
  1643. X            goto fail;
  1644. X          break;
  1645. X
  1646. X        case wordchar:
  1647. X          mcnt = (int) Sword;
  1648. X          goto matchsyntax;
  1649. X
  1650. X        case syntaxspec:
  1651. X          mcnt = *p++;
  1652. X        matchsyntax:
  1653. X          PREFETCH;
  1654. X          if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
  1655. X          break;
  1656. X
  1657. X        case notwordchar:
  1658. X          mcnt = (int) Sword;
  1659. X          goto matchnotsyntax;
  1660. X
  1661. X        case notsyntaxspec:
  1662. X          mcnt = *p++;
  1663. X        matchnotsyntax:
  1664. X          PREFETCH;
  1665. X          if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
  1666. X          break;
  1667. X#else
  1668. X        case wordchar:
  1669. X          PREFETCH;
  1670. X          if (SYNTAX (*d++) == 0) goto fail;
  1671. X          break;
  1672. X
  1673. X        case notwordchar:
  1674. X          PREFETCH;
  1675. X          if (SYNTAX (*d++) != 0) goto fail;
  1676. X          break;
  1677. X#endif /* not emacs */
  1678. X
  1679. X        case begbuf:
  1680. X          if (d == string1)     /* Note, d cannot equal string2 */
  1681. X            break;              /* unless string1 == string2.  */
  1682. X          goto fail;
  1683. X
  1684. X        case endbuf:
  1685. X          if (d == end2 || (d == end1 && size2 == 0))
  1686. X            break;
  1687. X          goto fail;
  1688. X
  1689. X        case exactn:
  1690. X          /* Match the next few pattern characters exactly.
  1691. X             mcnt is how many characters to match. */
  1692. X          mcnt = *p++;
  1693. X          if (translate)
  1694. X            {
  1695. X              do
  1696. X                {
  1697. X                  PREFETCH;
  1698. X                  if (translate[*d++] != *p++) goto fail;
  1699. X                }
  1700. X              while (--mcnt);
  1701. X            }
  1702. X          else
  1703. X            {
  1704. X              do
  1705. X                {
  1706. X                  PREFETCH;
  1707. X                  if (*d++ != *p++) goto fail;
  1708. X                }
  1709. X              while (--mcnt);
  1710. X            }
  1711. X          break;
  1712. X        }
  1713. X      continue;    /* Successfully matched one pattern command; keep matching */
  1714. X
  1715. X      /* Jump here if any matching operation fails. */
  1716. X    fail:
  1717. X      if (stackp != stackb)
  1718. X        /* A restart point is known.  Restart there and pop it. */
  1719. X        {
  1720. X          if (!stackp[-2])
  1721. X            {   /* If innermost failure point is dormant, flush it and keep looking */
  1722. X              stackp -= 2;
  1723. X              goto fail;
  1724. X            }
  1725. X          d = *--stackp;
  1726. X          p = *--stackp;
  1727. X          if (d >= string1 && d <= end1)
  1728. X            dend = end_match_1;
  1729. X        }
  1730. X      else break;   /* Matching at this starting point really fails! */
  1731. X    }
  1732. X  return -1;         /* Failure to match */
  1733. X}
  1734. X
  1735. Xstatic int
  1736. Xbcmp_translate (s1, s2, len, translate)
  1737. X     unsigned char *s1, *s2;
  1738. X     register int len;
  1739. X     unsigned char *translate;
  1740. X{
  1741. X  register unsigned char *p1 = s1, *p2 = s2;
  1742. X  while (len)
  1743. X    {
  1744. X      if (translate [*p1++] != translate [*p2++]) return 1;
  1745. X      len--;
  1746. X    }
  1747. X  return 0;
  1748. X}
  1749. X
  1750. X/* Entry points compatible with bsd4.2 regex library */
  1751. X
  1752. X#ifndef emacs
  1753. X
  1754. Xstatic struct re_pattern_buffer re_comp_buf;
  1755. X
  1756. Xchar *
  1757. Xre_comp (s)
  1758. X     char *s;
  1759. X{
  1760. X  if (!s)
  1761. X    {
  1762. X      if (!re_comp_buf.buffer)
  1763. X        return "No previous regular expression";
  1764. X      return 0;
  1765. X    }
  1766. X
  1767. X  if (!re_comp_buf.buffer)
  1768. X    {
  1769. X      if (!(re_comp_buf.buffer = (char *) malloc (200)))
  1770. X        return "Memory exhausted";
  1771. X      re_comp_buf.allocated = 200;
  1772. X      if (!(re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH)))
  1773. X        return "Memory exhausted";
  1774. X    }
  1775. X  return re_compile_pattern (s, strlen (s), &re_comp_buf);
  1776. X}
  1777. X
  1778. Xint
  1779. Xre_exec (s)
  1780. X     char *s;
  1781. X{
  1782. X  int len = strlen (s);
  1783. X  return 0 <= re_search (&re_comp_buf, s, len, 0, len, 0);
  1784. X}
  1785. X
  1786. X#endif /* emacs */
  1787. X
  1788. X#ifdef test
  1789. X
  1790. X#include <stdio.h>
  1791. X
  1792. X/* Indexed by a character, gives the upper case equivalent of the character */
  1793. X
  1794. Xstatic char upcase[0400] =
  1795. X  { 000, 001, 002, 003, 004, 005, 006, 007,
  1796. X    010, 011, 012, 013, 014, 015, 016, 017,
  1797. X    020, 021, 022, 023, 024, 025, 026, 027,
  1798. X    030, 031, 032, 033, 034, 035, 036, 037,
  1799. X    040, 041, 042, 043, 044, 045, 046, 047,
  1800. X    050, 051, 052, 053, 054, 055, 056, 057,
  1801. X    060, 061, 062, 063, 064, 065, 066, 067,
  1802. X    070, 071, 072, 073, 074, 075, 076, 077,
  1803. X    0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  1804. X    0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  1805. X    0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  1806. X    0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  1807. X    0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  1808. X    0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  1809. X    0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  1810. X    0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  1811. X    0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  1812. X    0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  1813. X    0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  1814. X    0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  1815. X    0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  1816. X    0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  1817. X    0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  1818. X    0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  1819. X    0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  1820. X    0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  1821. X    0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  1822. X    0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  1823. X    0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  1824. X    0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  1825. X    0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  1826. X    0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  1827. X  };
  1828. X
  1829. Xmain (argc, argv)
  1830. X     int argc;
  1831. X     char **argv;
  1832. X{
  1833. X  char pat[80];
  1834. X  struct re_pattern_buffer buf;
  1835. X  int i;
  1836. X  char c;
  1837. X  char fastmap[(1 << BYTEWIDTH)];
  1838. X
  1839. X  /* Allow a command argument to specify the style of syntax.  */
  1840. X  if (argc > 1)
  1841. X    obscure_syntax = atoi (argv[1]);
  1842. X
  1843. X  buf.allocated = 40;
  1844. X  buf.buffer = (char *) malloc (buf.allocated);
  1845. X  buf.fastmap = fastmap;
  1846. X  buf.translate = upcase;
  1847. X
  1848. X  while (1)
  1849. X    {
  1850. X      gets (pat);
  1851. X
  1852. X      if (*pat)
  1853. X        {
  1854. X          re_compile_pattern (pat, strlen(pat), &buf);
  1855. X
  1856. X          for (i = 0; i < buf.used; i++)
  1857. X            printchar (buf.buffer[i]);
  1858. X
  1859. X          putchar ('\n');
  1860. X
  1861. X          printf ("%d allocated, %d used.\n", buf.allocated, buf.used);
  1862. X
  1863. X          re_compile_fastmap (&buf);
  1864. X          printf ("Allowed by fastmap: ");
  1865. X          for (i = 0; i < (1 << BYTEWIDTH); i++)
  1866. X            if (fastmap[i]) printchar (i);
  1867. X          putchar ('\n');
  1868. X        }
  1869. X
  1870. X      gets (pat);       /* Now read the string to match against */
  1871. X
  1872. X      i = re_match (&buf, pat, strlen (pat), 0, 0);
  1873. X      printf ("Match value %d.\n", i);
  1874. X    }
  1875. X}
  1876. X
  1877. X#ifdef NOTDEF
  1878. Xprint_buf (bufp)
  1879. X     struct re_pattern_buffer *bufp;
  1880. X{
  1881. X  int i;
  1882. X
  1883. X  printf ("buf is :\n----------------\n");
  1884. X  for (i = 0; i < bufp->used; i++)
  1885. X    printchar (bufp->buffer[i]);
  1886. X
  1887. X  printf ("\n%d allocated, %d used.\n", bufp->allocated, bufp->used);
  1888. X
  1889. X  printf ("Allowed by fastmap: ");
  1890. X  for (i = 0; i < (1 << BYTEWIDTH); i++)
  1891. X    if (bufp->fastmap[i])
  1892. X      printchar (i);
  1893. X  printf ("\nAllowed by translate: ");
  1894. X  if (bufp->translate)
  1895. X    for (i = 0; i < (1 << BYTEWIDTH); i++)
  1896. X      if (bufp->translate[i])
  1897. X        printchar (i);
  1898. X  printf ("\nfastmap is%s accurate\n", bufp->fastmap_accurate ? "" : "n't");
  1899. X  printf ("can %s be null\n----------", bufp->can_be_null ? "" : "not");
  1900. X}
  1901. X#endif
  1902. X
  1903. Xprintchar (c)
  1904. X     char c;
  1905. X{
  1906. X  if (c < 041 || c >= 0177)
  1907. X    {
  1908. X      putchar ('\\');
  1909. X      putchar (((c >> 6) & 3) + '0');
  1910. X      putchar (((c >> 3) & 7) + '0');
  1911. X      putchar ((c & 7) + '0');
  1912. X    }
  1913. X  else
  1914. X    putchar (c);
  1915. X}
  1916. X
  1917. Xerror (string)
  1918. X     char *string;
  1919. X{
  1920. X  puts (string);
  1921. X  exit (1);
  1922. X}
  1923. X
  1924. X#endif /* test */
  1925. SHAR_EOF
  1926. echo "extracting regex.h"
  1927. sed 's/^X//' << \SHAR_EOF > regex.h
  1928. X/* Definitions for data structures callers pass the regex library.
  1929. X   Copyright (C) 1985 Free Software Foundation, Inc.
  1930. X
  1931. X               NO WARRANTY
  1932. X
  1933. X  BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  1934. XNO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  1935. XWHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  1936. XRICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  1937. XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  1938. XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  1939. XFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  1940. XAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  1941. XDEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  1942. XCORRECTION.
  1943. X
  1944. X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  1945. XSTALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  1946. XWHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  1947. XLIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  1948. XOTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  1949. XUSE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  1950. XDATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  1951. XA FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  1952. XPROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  1953. XDAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  1954. X
  1955. X        GENERAL PUBLIC LICENSE TO COPY
  1956. X
  1957. X  1. You may copy and distribute verbatim copies of this source file
  1958. Xas you receive it, in any medium, provided that you conspicuously and
  1959. Xappropriately publish on each copy a valid copyright notice "Copyright
  1960. X(C) 1985 Free Software Foundation, Inc."; and include following the
  1961. Xcopyright notice a verbatim copy of the above disclaimer of warranty
  1962. Xand of this License.  You may charge a distribution fee for the
  1963. Xphysical act of transferring a copy.
  1964. X
  1965. X  2. You may modify your copy or copies of this source file or
  1966. Xany portion of it, and copy and distribute such modifications under
  1967. Xthe terms of Paragraph 1 above, provided that you also do the following:
  1968. X
  1969. X    a) cause the modified files to carry prominent notices stating
  1970. X    that you changed the files and the date of any change; and
  1971. X
  1972. X    b) cause the whole of any work that you distribute or publish,
  1973. X    that in whole or in part contains or is a derivative of this
  1974. X    program or any part thereof, to be licensed at no charge to all
  1975. X    third parties on terms identical to those contained in this
  1976. X    License Agreement (except that you may choose to grant more extensive
  1977. X    warranty protection to some or all third parties, at your option).
  1978. X
  1979. X    c) You may charge a distribution fee for the physical act of
  1980. X    transferring a copy, and you may at your option offer warranty
  1981. X    protection in exchange for a fee.
  1982. X
  1983. XMere aggregation of another unrelated program with this program (or its
  1984. Xderivative) on a volume of a storage or distribution medium does not bring
  1985. Xthe other program under the scope of these terms.
  1986. X
  1987. X  3. You may copy and distribute this program (or a portion or derivative
  1988. Xof it, under Paragraph 2) in object code or executable form under the terms
  1989. Xof Paragraphs 1 and 2 above provided that you also do one of the following:
  1990. X
  1991. X    a) accompany it with the complete corresponding machine-readable
  1992. X    source code, which must be distributed under the terms of
  1993. X    Paragraphs 1 and 2 above; or,
  1994. X
  1995. X    b) accompany it with a written offer, valid for at least three
  1996. X    years, to give any third party free (except for a nominal
  1997. X    shipping charge) a complete machine-readable copy of the
  1998. X    corresponding source code, to be distributed under the terms of
  1999. X    Paragraphs 1 and 2 above; or,
  2000. X
  2001. X    c) accompany it with the information you received as to where the
  2002. X    corresponding source code may be obtained.  (This alternative is
  2003. X    allowed only for noncommercial distribution and only if you
  2004. X    received the program in object code or executable form alone.)
  2005. X
  2006. XFor an executable file, complete source code means all the source code for
  2007. Xall modules it contains; but, as a special exception, it need not include
  2008. Xsource code for modules which are standard libraries that accompany the
  2009. Xoperating system on which the executable file runs.
  2010. X
  2011. X  4. You may not copy, sublicense, distribute or transfer this program
  2012. Xexcept as expressly provided under this License Agreement.  Any attempt
  2013. Xotherwise to copy, sublicense, distribute or transfer this program is void and
  2014. Xyour rights to use the program under this License agreement shall be
  2015. Xautomatically terminated.  However, parties who have received computer
  2016. Xsoftware programs from you with this License Agreement will not have
  2017. Xtheir licenses terminated so long as such parties remain in full compliance.
  2018. X
  2019. X  5. If you wish to incorporate parts of this program into other free
  2020. Xprograms whose distribution conditions are different, write to the Free
  2021. XSoftware Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  2022. Xworked out a simple rule that can be stated here, but we will often permit
  2023. Xthis.  We will be guided by the two goals of preserving the free status of
  2024. Xall derivatives of our free software and of promoting the sharing and reuse of
  2025. Xsoftware.
  2026. X
  2027. X
  2028. XIn other words, you are welcome to use, share and improve this program.
  2029. XYou are forbidden to forbid anyone else to use, share and improve
  2030. Xwhat you give them.   Help stamp out software-hoarding!  */
  2031. X
  2032. X
  2033. X/* Define number of parens for which we record the beginnings and ends.
  2034. X   This affects how much space the `struct re_registers' type takes up.  */
  2035. X#ifndef RE_NREGS
  2036. X#define RE_NREGS 10
  2037. X#endif
  2038. X
  2039. X/* These bits are used in the obscure_syntax variable to choose among
  2040. X   alternative regexp syntaxes.  */
  2041. X
  2042. X/* 1 means plain parentheses serve as grouping, and backslash
  2043. X     parentheses are needed for literal searching.
  2044. X   0 means backslash-parentheses are grouping, and plain parentheses
  2045. X     are for literal searching.  */
  2046. X#define RE_NO_BK_PARENS 1
  2047. X
  2048. X/* 1 means plain | serves as the "or"-operator, and \| is a literal.
  2049. X   0 means \| serves as the "or"-operator, and | is a literal.  */
  2050. X#define RE_NO_BK_VBAR 2
  2051. X
  2052. X/* 0 means plain + or ? serves as an operator, and \+, \? are literals.
  2053. X   1 means \+, \? are operators and plain +, ? are literals.  */
  2054. X#define RE_BK_PLUS_QM 4
  2055. X
  2056. X/* 1 means | binds tighter than ^ or $.
  2057. X   0 means the contrary.  */
  2058. X#define RE_TIGHT_VBAR 8
  2059. X
  2060. X/* 1 means treat \n as an _OR operator
  2061. X   0 means treat it as a normal character */
  2062. X#define RE_NEWLINE_OR 16
  2063. X
  2064. X/* 0 means that a special characters (such as *, ^, and $) always have
  2065. X     their special meaning regardless of the surrounding context.
  2066. X   1 means that special characters may act as normal characters in some
  2067. X     contexts.  Specifically, this applies to:
  2068. X    ^ - only special at the beginning, or after ( or |
  2069. X    $ - only special at the end, or before ) or |
  2070. X    *, +, ? - only special when not after the beginning, (, or | */
  2071. X#define RE_CONTEXT_INDEP_OPS 32
  2072. X
  2073. X/* Now define combinations of bits for the standard possibilities.  */
  2074. X#define RE_SYNTAX_AWK (RE_NO_BK_PARENS | RE_NO_BK_VBAR | RE_CONTEXT_INDEP_OPS)
  2075. X#define RE_SYNTAX_EGREP (RE_SYNTAX_AWK | RE_NEWLINE_OR)
  2076. X#define RE_SYNTAX_GREP (RE_BK_PLUS_QM | RE_NEWLINE_OR)
  2077. X#define RE_SYNTAX_EMACS 0
  2078. X
  2079. X/* This data structure is used to represent a compiled pattern. */
  2080. X
  2081. Xstruct re_pattern_buffer
  2082. X  {
  2083. X    char *buffer;    /* Space holding the compiled pattern commands. */
  2084. X    int allocated;    /* Size of space that  buffer  points to */
  2085. X    int used;        /* Length of portion of buffer actually occupied */
  2086. X    char *fastmap;    /* Pointer to fastmap, if any, or zero if none. */
  2087. X            /* re_search uses the fastmap, if there is one,
  2088. X               to skip quickly over totally implausible characters */
  2089. X    char *translate;    /* Translate table to apply to all characters before comparing.
  2090. X               Or zero for no translation.
  2091. X               The translation is applied to a pattern when it is compiled
  2092. X               and to data when it is matched. */
  2093. X    char fastmap_accurate;
  2094. X            /* Set to zero when a new pattern is stored,
  2095. X               set to one when the fastmap is updated from it. */
  2096. X    char can_be_null;   /* Set to one by compiling fastmap
  2097. X               if this pattern might match the null string.
  2098. X               It does not necessarily match the null string
  2099. X               in that case, but if this is zero, it cannot.
  2100. X               2 as value means can match null string
  2101. X               but at end of range or before a character
  2102. X               listed in the fastmap.  */
  2103. X  };
  2104. X
  2105. X/* Structure to store "register" contents data in.
  2106. X
  2107. X   Pass the address of such a structure as an argument to re_match, etc.,
  2108. X   if you want this information back.
  2109. X
  2110. X   start[i] and end[i] record the string matched by \( ... \) grouping i,
  2111. X   for i from 1 to RE_NREGS - 1.
  2112. X   start[0] and end[0] record the entire string matched. */
  2113. X
  2114. Xstruct re_registers
  2115. X  {
  2116. X    int start[RE_NREGS];
  2117. X    int end[RE_NREGS];
  2118. X  };
  2119. X
  2120. X/* These are the command codes that appear in compiled regular expressions, one per byte.
  2121. X  Some command codes are followed by argument bytes.
  2122. X  A command code can specify any interpretation whatever for its arguments.
  2123. X  Zero-bytes may appear in the compiled regular expression. */
  2124. X
  2125. Xenum regexpcode
  2126. X  {
  2127. X    unused,
  2128. X    exactn,    /* followed by one byte giving n, and then by n literal bytes */
  2129. X    begline,   /* fails unless at beginning of line */
  2130. X    endline,   /* fails unless at end of line */
  2131. X    jump,     /* followed by two bytes giving relative address to jump to */
  2132. X    on_failure_jump,     /* followed by two bytes giving relative address of place
  2133. X                    to resume at in case of failure. */
  2134. X    finalize_jump,     /* Throw away latest failure point and then jump to address. */
  2135. X    maybe_finalize_jump, /* Like jump but finalize if safe to do so.
  2136. X                This is used to jump back to the beginning
  2137. X                of a repeat.  If the command that follows
  2138. X                this jump is clearly incompatible with the
  2139. X                one at the beginning of the repeat, such that
  2140. X                we can be sure that there is no use backtracking
  2141. X                out of repetitions already completed,
  2142. X                then we finalize. */
  2143. X    dummy_failure_jump,  /* jump, and push a dummy failure point.
  2144. X                This failure point will be thrown away
  2145. X                if an attempt is made to use it for a failure.
  2146. X                A + construct makes this before the first repeat.  */
  2147. X    anychar,     /* matches any one character */
  2148. X    charset,     /* matches any one char belonging to specified set.
  2149. X            First following byte is # bitmap bytes.
  2150. X            Then come bytes for a bit-map saying which chars are in.
  2151. X            Bits in each byte are ordered low-bit-first.
  2152. X            A character is in the set if its bit is 1.
  2153. X            A character too large to have a bit in the map
  2154. X            is automatically not in the set */
  2155. X    charset_not, /* similar but match any character that is NOT one of those specified */
  2156. X    start_memory, /* starts remembering the text that is matched
  2157. X            and stores it in a memory register.
  2158. X            followed by one byte containing the register number.
  2159. X            Register numbers must be in the range 0 through NREGS. */
  2160. X    stop_memory, /* stops remembering the text that is matched
  2161. X            and stores it in a memory register.
  2162. X            followed by one byte containing the register number.
  2163. X            Register numbers must be in the range 0 through NREGS. */
  2164. X    duplicate,    /* match a duplicate of something remembered.
  2165. X            Followed by one byte containing the index of the memory register. */
  2166. X    before_dot,     /* Succeeds if before dot */
  2167. X    at_dot,     /* Succeeds if at dot */
  2168. X    after_dot,     /* Succeeds if after dot */
  2169. X    begbuf,      /* Succeeds if at beginning of buffer */
  2170. X    endbuf,      /* Succeeds if at end of buffer */
  2171. X    wordchar,    /* Matches any word-constituent character */
  2172. X    notwordchar, /* Matches any char that is not a word-constituent */
  2173. X    wordbeg,     /* Succeeds if at word beginning */
  2174. X    wordend,     /* Succeeds if at word end */
  2175. X    wordbound,   /* Succeeds if at a word boundary */
  2176. X    notwordbound, /* Succeeds if not at a word boundary */
  2177. X    syntaxspec,  /* Matches any character whose syntax is specified.
  2178. X            followed by a byte which contains a syntax code, Sword or such like */
  2179. X    notsyntaxspec /* Matches any character whose syntax differs from the specified. */
  2180. X  };
  2181. X
  2182. Xextern char *re_compile_pattern ();
  2183. X/* Is this really advertised? */
  2184. Xextern void re_compile_fastmap ();
  2185. Xextern int re_search (), re_search_2 ();
  2186. Xextern int re_match (), re_match_2 ();
  2187. X
  2188. X/* 4.2 bsd compatibility (yuck) */
  2189. Xextern char *re_comp ();
  2190. Xextern int re_exec ();
  2191. X
  2192. X#ifdef SYNTAX_TABLE
  2193. Xextern char *re_syntax_table;
  2194. X#endif
  2195. SHAR_EOF
  2196. echo "End of archive 2 (of 3)"
  2197. # if you want to concatenate archives, remove anything after this line
  2198. exit
  2199.